const App = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		items: [{
				value: '1',
				name: '1星'
			},
			{
				value: '2',
				name: '2星'
			},
			{
				value: '3',
				name: '3星'
			},
			{
				value: '4',
				name: '4星'
			},
			{
				value: '5',
				name: '5星',
				checked: 'true'
			}
		],
		starIndex2: 5,
		stars: 5,
		order_id: 0,
		order_ids: 0,
		name: '',
		price: '',
		image: '',
		page_list: [],
		is_fw: false,
		is_text: '确定完成!',
		image_list: [],
		img_ids: '',
		ping: '',
	},
	SubmitFuwu: function() {
		wx.navigateTo({
			//url: './comment/comment?order_id=' + _this.data.order_ids,
			url: './comment/comment?order_id=' + _this.data.order_ids,
		})
		// let _this = this;
		// var order_id = this.data.order_ids
		// App._post_form('appointment/confirm_finish', {
		// 	order_id: order_id
		// }, function(result) {
		// 	if (result.msg = 'success') {
		// 		wx.navigateTo({
		// 		  url: './comment/comment?order_id=' + _this.data.order_ids,
		// 		})
		// 	}
		// });
	},
	QxFuwu: function() {
		this.setData({
			is_fw: false,
			order_ids: 0
		})
	},
	QxPing: function() {
		this.setData({
			is_done: 0
		})
	},
	QdWc: function(e) {
		var order_id = e.currentTarget.id
		this.setData({
			is_fw: true,
			order_ids: order_id
		})
	},
	onChange2(e) {
		const index = e.detail.index;
		this.setData({
			'starIndex2': index
		})
	},
	radioChange(e) {
		console.log('radio发生change事件，携带value值为：', e.detail.value)
		this.setData({
			stars: e.detail.value
		})
		const items = this.data.items
		for (let i = 0, len = items.length; i < len; ++i) {
			items[i].checked = items[i].value === e.detail.value
		}
		this.setData({
			items
		})
	},
	pingjia: function(e) {
		this.setData({
			ping: e.detail.value
		})
		console.log(this.data.ping)
	},
	/**
	 * 选择图片
	 */
	chooseImage: function(e) {
		console.log("choose")
		let _this = this,
			index = e.currentTarget.dataset.index
		var imageList = _this.data.image_list
		// 选择图片
		wx.chooseImage({
			count: 6 - imageList.length,
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function(res) {
				console.log(res)
				_this.setData({
					image_list: imageList.concat(res.tempFilePaths)
				});
				console.log(_this.data.image_list)
			}
		});
	},

	/**
	 * 删除图片
	 */
	deleteImage: function(e) {
		let dataset = e.currentTarget.dataset,
			imagelist = this.data.image_list;
		imagelist.splice(dataset.imageIndex, 1);
		this.setData({
			image_list: imagelist
		});
	},

	SubmitPing: function() {
		let _this = this;
		// 统计图片数量
		let imagesLength = _this.data.image_list.length;
		// 判断是否需要上传图片
		if (imagesLength > 0) {
			_this.data.img_ids = ''
			// POST 参数
			let params = {
				wxapp_id: App.getWxappId(),
				token: wx.getStorageSync('token')
			};
			// 文件上传
			let i = 0;
			_this.data.image_list.forEach(function(filePath, fileKey) {
				wx.uploadFile({
					url: App.api_root + 'upload/image',
					filePath: filePath,
					name: 'iFile',
					formData: params,
					success: function(res) {
						let result = typeof res.data === "object" ? res.data : JSON
							.parse(res.data);
						if (result.code === 1) {
							if (i == 0) {
								var id = result.data.file_id
							} else {
								var id = ',' + result.data.file_id
							}
							_this.data.img_ids += id
							console.log(_this.data.img_ids)
							//item[fileKey] = result.data.file_id;
						}
					},
					complete: function() {
						i++;
						console.log(imagesLength)
						console.log(i)
						if (imagesLength === i) {
							// 所有文件上传完成
							console.log('upload complete');
							// 执行回调函数
							App._post_form('appointment/ping_finish', {
								stars: _this.data.starIndex2,
								ping: _this.data.ping,
								order_id: _this.data.order_id,
								img_ids: _this.data.img_ids,
							}, function(result) {
								if (result.msg = 'success') {
									wx.reLaunch({
										url: '/pages/user/index'
									})
								}
							});
						}
					}
				});
			});
		} else {
			App._post_form('appointment/ping_finish', {
				stars: _this.data.starIndex2,
				ping: _this.data.ping,
				order_id: _this.data.order_id
			}, function(result) {
				if (result.msg = 'success') {
					wx.reLaunch({
						url: '/pages/user/index'
					})

				}
			});
		}
	},
	getOrder: function() {
		console.log("getOrder")
		var order_id = this.data.order_id
		let _this = this;
		App._post_form('appointment/getGoodsInfo', {
			order_id: order_id
		}, function(result) {
			_this.setData({
				name: result.goods_name,
				image: result.goods_image,
				price: result.goods_sku.goods_price
			})
		});
	
	},
	onLoad: function(options) {
		var order_id = options.order_id
		this.setData({
			order_id: order_id
		})
		this.getOrder()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		//this.getOrder()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
